package com.huanying.goodjob.prize;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;
import com.huanying.goodjob.lottery.Lottery;

@Service
public class PrizeServiceImpl implements PrizeService{

	@Autowired
	private BaseDao dao;
	@Override
	public void add_prize(Prize prize) throws Exception {
		// TODO Auto-generated method stub
		dao.save(prize);
	}

	@Override
	public void update_prize(Prize prize) throws Exception {
		// TODO Auto-generated method stub
		dao.update(prize);
	}

	@Override
	public void delete_prize(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Prize set status=0 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
		dao.exculeByParams(hql, param);
	}

	@Override
	public Prize get_prize(int id) throws Exception {
		// TODO Auto-generated method stub
		return (Prize)dao.load(Prize.class, id);
	}

	@Override
	public PageBean search_prize(Map<String, Object> map, int pageSize, int page) throws Exception {
		StringBuffer hql=new StringBuffer("from Prize as p where 1=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("name")!=null && map.get("name")!=""){
			hql.append(" and p.name like ?");
			param.add("%"+map.get("name")+"%");
		}
		if(map.get("chance")!=null && map.get("chance")!=""){
			hql.append(" and p.chance<=?");
			param.add(Double.parseDouble((String)map.get("chance")));
		}
		if(map.get("price")!=null && map.get("price")!="")
		{
			hql.append(" and p.price=?");
			param.add(Double.parseDouble((String)map.get("price")));
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public List get_prizeList() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Prize where status=1";
		List<Object> param=new ArrayList<Object>();
		return dao.exculeHqlByParams(hql, param);
	}

}
